package cn.iocoder.yudao.module.fm.service.fileinfo;

import cn.iocoder.yudao.module.fm.controller.admin.fileinfourl.vo.FileInfoUrlCreateReqVO;
import cn.iocoder.yudao.module.fm.controller.admin.fileinfourl.vo.FileInfoUrlRespVO;
import cn.iocoder.yudao.module.fm.convert.fileinfourl.FileInfoUrlConvert;
import cn.iocoder.yudao.module.fm.dal.dataobject.fileinfourl.FileInfoUrlDO;
import cn.iocoder.yudao.module.fm.dal.mysql.fileinfourl.FileInfoUrlMapper;
import cn.iocoder.yudao.module.fm.enums.FileInfoType;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;

import java.util.*;

import cn.iocoder.yudao.module.fm.controller.admin.fileinfo.vo.*;
import cn.iocoder.yudao.module.fm.dal.dataobject.fileinfo.FileInfoDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.fm.convert.fileinfo.FileInfoConvert;
import cn.iocoder.yudao.module.fm.dal.mysql.fileinfo.FileInfoMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.fm.enums.ErrorCodeConstants.*;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;

/**
 * 文件信息 Service 实现类
 *
 * @author 档案
 */
@Service
@Validated
public class FileInfoServiceImpl implements FileInfoService {

    @Resource
    private FileInfoMapper fileInfoMapper;
    @Resource
    private FileInfoUrlMapper fileInfoUrlMapper;

    @Override
    public void save(List<FileInfoCreateReqVO> fileList, Long parentId, Long parentType) {
        if (fileList == null) {
            return;
        }
        for (FileInfoCreateReqVO fileInfoCreateReqVO : fileList) {

            FileInfoDO fileInfoDO = FileInfoConvert.INSTANCE.convert(fileInfoCreateReqVO);
            fileInfoDO.setParentId(parentId);
            fileInfoDO.setParentType(parentType);

            Long fileInfoId = fileInfoDO.getId();
            if (fileInfoId == null) {
                fileInfoMapper.insert(fileInfoDO);
                fileInfoId = fileInfoDO.getId();
            } else {
                fileInfoMapper.updateById(fileInfoDO);
            }


            // 删除之前的数据
            fileInfoUrlMapper.delete(Wrappers.<FileInfoUrlDO>lambdaQuery().eq(FileInfoUrlDO::getParentId, fileInfoId));

            // 新增
            List<FileInfoUrlCreateReqVO> fileUrlList = fileInfoCreateReqVO.getFileUrlList();
            for (FileInfoUrlCreateReqVO fileInfoUrlCreateReqVO : fileUrlList) {
                FileInfoUrlDO fileInfoUrlDO = FileInfoUrlConvert.INSTANCE.convert(fileInfoUrlCreateReqVO);
                fileInfoUrlDO.setParentId(fileInfoId);
                fileInfoUrlDO.setId(null);
                fileInfoUrlMapper.insert(fileInfoUrlDO);
            }
        }
    }

    @Override
    public List<FileInfoRespVO> get(Long parentId, Long parentType) {
        List<FileInfoDO> fileInfoDOS = fileInfoMapper.selectList(Wrappers.<FileInfoDO>lambdaQuery()
                .eq(FileInfoDO::getParentId, parentId)
                .eq(FileInfoDO::getParentType, parentType)
        );

        List<FileInfoRespVO> fileInfoRespVOS = FileInfoConvert.INSTANCE.convertList(fileInfoDOS);

        for (FileInfoRespVO fileInfoRespVO : fileInfoRespVOS) {

            List<FileInfoUrlRespVO> fileInfoUrlRespVOS = FileInfoUrlConvert.INSTANCE.convertList(fileInfoUrlMapper.selectList(Wrappers.<FileInfoUrlDO>lambdaQuery().eq(FileInfoUrlDO::getParentId, fileInfoRespVO.getId())));

            fileInfoRespVO.setFileUrlList(fileInfoUrlRespVOS);
        }

        return fileInfoRespVOS;
    }


    @Override
    public Long createFileInfo(FileInfoCreateReqVO createReqVO) {
        // 插入
        FileInfoDO fileInfo = FileInfoConvert.INSTANCE.convert(createReqVO);
        fileInfoMapper.insert(fileInfo);
        // 返回
        return fileInfo.getId();
    }

    @Override
    public void updateFileInfo(FileInfoUpdateReqVO updateReqVO) {
        // 校验存在
        validateFileInfoExists(updateReqVO.getId());
        // 更新
        FileInfoDO updateObj = FileInfoConvert.INSTANCE.convert(updateReqVO);
        fileInfoMapper.updateById(updateObj);
    }

    @Override
    public void deleteFileInfo(Long id) {
        // 校验存在
        validateFileInfoExists(id);
        // 删除
        fileInfoMapper.deleteById(id);
    }

    private void validateFileInfoExists(Long id) {
        if (fileInfoMapper.selectById(id) == null) {
            throw exception(DATA_NOT_EXISTS);
        }
    }

    @Override
    public FileInfoDO getFileInfo(Long id) {
        return fileInfoMapper.selectById(id);
    }

    @Override
    public List<FileInfoDO> getFileInfoList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return fileInfoMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<FileInfoDO> getFileInfoPage(FileInfoPageReqVO pageReqVO) {
        return fileInfoMapper.selectPage(pageReqVO);
    }

    @Override
    public List<FileInfoDO> getFileInfoList(FileInfoExportReqVO exportReqVO) {
        return fileInfoMapper.selectList(exportReqVO);
    }

}
